package com.chaosj.webmagic.dto.gushiwen;

import com.chaosj.webmagic.dto.SearchResponseDto;
import com.google.common.base.Splitter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @className: HomeResponseDto
 * @description:
 * @author: caoyangjie
 * @date: 2021/8/11
 **/
public class HomeResponseDto extends SearchResponseDto {

    private Map<String,List<AccessPageDto>> homeMap = new HashMap<>();

    @Override
    public StringBuffer buffer() {
        StringBuffer buffer = new StringBuffer();
        if ( !homeMap.isEmpty() ) {
            homeMap.forEach((k,v)->{
                buffer.append(String.format("**************************************%s start **************************************\n", k));
                AtomicInteger count = new AtomicInteger();
                v.stream().forEach(access->{
                    buffer.append(access).append("\n");
                });
                buffer.append("\n");
                buffer.append(String.format("**************************************%s end ****************************************\n", k));
            });
        }
        if ( !list.isEmpty() ) {
            list.forEach(poetry->buffer.append(poetry));
        }
        return buffer;
    }

    @Override
    public void addObject(String... args) {
        if( args[0].equals("access") ){
            addAccess(args[1], args[2], args[3]);
        }else{
            addPoetry(args[1], args[2], args[3], args[4], args[5]);
        }
    }

    public void addAccess(String title, String label, String target){
        title = title.trim();
        homeMap.computeIfAbsent(title,(t)-> new ArrayList<>()).add(new AccessPageDto(title,label,target));
    }

    public void addPoetry(String tip, String target, String author, String dynasty, String content){
        list.add(new ShiGeContentDto(tip, target, author, dynasty, content));
    }

    @Data
    @Accessors(chain = true)
    @AllArgsConstructor
    class AccessPageDto {
        private String type;
        private String label;
        private String target;

        @Override
        public String toString(){
            StringBuffer buffer = new StringBuffer();
            buffer.append(String.format("| %s | 访问地址: %s |", label, target));
            return buffer.toString();
        }
    }

    @Data
    @Accessors(chain = true)
    @AllArgsConstructor
    public class ShiGeContentDto {
        private String title;
        private String target;
        private String author;
        private String dynasty;
        private String content;

        @Override
        public String toString() {
            StringBuffer buffer = new StringBuffer();
            buffer.append(String.format("\n***********************************[ %s ] start ***********************************\n",title));
            buffer.append("访问地址：").append(target).append("\n");
            buffer.append(String.format("******           %s             %s   ********\n",author, dynasty));
            Splitter.on("。").trimResults().omitEmptyStrings().splitToList(content).forEach(line->{
                buffer.append(line).append("。\n");
            });
            buffer.append(String.format("***********************************[ %s ] end *************************************\n",title));
            return buffer.toString();
        }
    }
}
